Addrrules<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- cart卡片 -->
    <el-card class="box-card">
      <!-- 搜索、添加商品 -->
      <el-row>
        <el-col :span="11"
          ><el-input
            :span="9"
            placeholder="请输入内容"
            v-model="query.query"
            class="input-with-select"
            clearable
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button> </el-input
        ></el-col>
        <el-col :span="8">
          <el-button
            :span="6"
            type="primary"
            user-select
            @click="addGoods"
            class="addGoods"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table style="width: 100%" border :data="goodsListData">
        <!-- <el-table-column type="index"></el-table-column> -->
        <el-table-column type="index" width="50" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="120">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="95">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160">
          <template slot-scope="scope">
            {{ scope.row.add_time | DateFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页效果 -->
      <el-pagination
        style="margin-top:10px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 搜索的值
      searchVal: "",
      query: {
        query: "",
        // 页码
        pagenum: 1,
        // 每页显示的条数
        pagesize: 10
      },
      // 表格商品数据
      goodsListData: [],
      // 保存商品的总数
      total: 0
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: ret } = await this.$http.get("/goods", {
        params: this.query
      });

      if (ret.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }

      // console.log(ret);
      this.goodsListData = ret.data.goods;
      console.log(this.goodsListData);
      this.total = ret.data.total;
    },
    // 当前显示多少条一页
    handleSizeChange(size) {
      this.query.pagesize = size;
      this.getGoodsList();
    },
    // 当前页发生了改变、
    handleCurrentChange(currentPage) {
      this.query.pagenum = currentPage;
      this.getGoodsList();
    },
    // 删除商品参数
    async deleteGoods(row) {
      const ret = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (ret !== "confirm") {
        return this.$message.error("取消删除");
      }
      const { data: val } = await this.$http.delete(`/goods/${row.goods_id}`);
      if (val.meta.status !== 200) {
        return this.$$message.error("删除失败");
      }
      this.$message.success(val.meta.msg);
      this.getGoodsList();
    },
    addGoods() {
      console.log("添加商品");
      this.$router.push("/goods/add");
    }
  }
};
</script>

<style lang="less" scope>
.addGoods {
  margin-left: 20px;
}
</style>
